import { reqGoodsInfo,reqAddOrUpdateShopCart} from "../api"
//封装游客身分模块---生成一个随机字符串
import { getUUID } from "../utils/uuid_token";
const state={
    goodInfo:{},
    //游客临时身份
    uuid_token : getUUID()
}
const mutations={
    GETGOODINFO(state,goodInfo){
        state.goodInfo = goodInfo
    }
}
const actions = {
    //获取产品信息的action
    async getGoodInfo({commit},skuId){
        let result = await reqGoodsInfo(skuId)
        if(result.code == 200){
            commit('GETGOODINFO',result.data)
        }
    },
    async addOrUpdateShopCart({commit},{skuId,skuNum}){
        let result = await reqAddOrUpdateShopCart(skuId,skuNum)
        if(result.code == 200){
            return "ok"
        }else{
            return Promise.reject(new Error('fail'))
        }
    }
}
const getters = {
    categoryView(state){
        return state.goodInfo.categoryView || {}
    },
    skuInfo(state){
        return state.goodInfo.skuInfo || {}
    },
    //售卖属性简化
    spuSaleAttrList(state){
        return state.goodInfo.spuSaleAttrList || []
    }
}


export default{
    state,
    mutations,
    actions,
    getters
}